package com.pzhu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pzhu.common.constant.Constant;
import com.pzhu.model.entity.Wage;
import com.pzhu.model.help.PageBean;
import com.pzhu.model.help.ResponseResult;
import com.pzhu.service.WageService;
import com.pzhu.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author : zqh
 * @Date : 2024/1/4 0004 下午 2:21
 * @Version: 1.0
 */
@Api(tags = "工资相关接口")
@RestController
@RequestMapping("/wage")
public class WageController {

    @Autowired
    private WageService wageService;

    @ApiOperation("查询所有工资信息")
    @PostMapping("/list")
    public ResponseResult list(@RequestBody PageBean pageBean){

        String query=pageBean.getQuery().trim();

        Page<Wage> page = wageService.page(
                new Page<>(pageBean.getPageNum(), pageBean.getPageSize()),
                new QueryWrapper<Wage>().like(StringUtil.isNotEmpty(query), "username", query));

        List<Wage> list = page.getRecords();
        Map<String,Object> map=new HashMap<>();
        map.put("wageList",list);
        map.put("total",page.getTotal());
        return ResponseResult.ok(map);
    }

    @ApiOperation("结算工资")
    @PostMapping("/foot/{id}")
    @ApiImplicitParam(name = "id",value = "工资条ID",required = true)
    public ResponseResult foot (@PathVariable(value = "id")  Long id) {
        UpdateWrapper<Wage> updateWrapper = Wrappers.update();
        updateWrapper.set("wage", Constant.BASE_WAGE);
        updateWrapper.eq("id",id);
        wageService.update(updateWrapper);
        return ResponseResult.ok();
    }
}
